<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Frequently Asked Questions</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Low-level-functions.xhtml" title="Deprecated low-level functions"/>
    <link rel="next" href="Framework.xhtml" title="The Kawa language framework"/>
  </head>
  <body>
    <header/>
    <section class="chapter" title="Frequently Asked Questions" epub:type="chapter" id="FAQs">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title">Frequently Asked Questions</h2>
          </div>
        </div>
      </div>
      <span id="importing-class-names"/>
      <h4 id="idm139667869230288">What is the equivalent of Java import?</h4>
      <p>To provide a short name for a class instead of the complete fully-qualified
name use either <code class="literal">define-alias</code> (or <code class="literal">define-private-alias</code>)
or the <code class="literal">import</code>-<code class="literal">class</code> combination.
For example, to be able to write <code class="literal">ArrayList</code> instead
of <code class="literal">java.util.ArrayList</code> do either:
</p>
      <pre class="screen">(import (class java.util ArrayList))
</pre>
      <p>or
</p>
      <pre class="screen">(define-alias ArrayList java.util.ArrayList)
</pre>
      <p>Using <code class="literal">import</code> is recommended:
It handles errors better,
and it allows you to define multiple aliases conveniently:
</p>
      <pre class="screen">(import (class java.util Map HashMap))
</pre>
      <p>Both forms allow renaming.  For example if you want to refer
to <code class="literal">java.lang.StringBuilder</code> as <code class="literal">StrBuf</code> do:
</p>
      <pre class="screen">(import (class java.lang (StringBuilder StrBuf)))
</pre>
      <p>or:
</p>
      <pre class="screen">(define-alias StrBuf java.lang.StringBuilder)
</pre>
      <p>The name(s) defined by <code class="literal">import</code> are by default private.
A name defined using <code class="literal">define-alias</code> is by default exported;
to avoid that use <code class="literal">define-private-alias</code> instead.
</p>
      <p>You can also use <code class="literal">define-namespace</code> to introduce an abbreviation or
renaming of a class name, but as a matter of style <code class="literal">define-alias</code>
is preferred.
</p>
      <p>There is no direct equivalent to Java’s <code class="literal">import PackageOrTypeName.*</code>
(type-import-on-demand) declaration, but you can alias a package:
</p>
      <pre class="screen">(define-alias jutil java.util)
(define mylist :: jutil:List (jutil:ArrayList))
</pre>
      <p>To import a static member, giving it a shortened name
(like Java’s static-import-on-demand declaration), you can use
<code class="literal">define-alias</code>.  For example:
</p>
      <pre class="screen">(define-alias console java.lang.System:console)
</pre>
      <p>For static fields only (not methods or member classes) you can
use an <code class="literal">import</code> form, either:
</p>
      <pre class="screen">(import (only (java lang System) out))
</pre>
      <p>or:
</p>
      <pre class="screen">(import (only java.lang.System out))
</pre>
      <p>This works because Kawa can treat any class as a “library”;
in which case it considers all public static fields as exported bindings.
</p>
      <h4 id="idm139667869213824">How do I refer to a Java member (nested) class?</h4>
      <p>Consider the Java SE member class <code class="literal">javax.swing.text.AbstractDocument.Content</code>.
Using the Java syntax doesn’t work in Kawa.
Inside you should use Kawa’s colon operator:
</p>
      <pre class="screen">javax.swing.text.AbstractDocument:Content
</pre>
      <p>Alternatively, you can use the internal JVM class name:
</p>
      <pre class="screen">javax.swing.text.AbstractDocument$Content
</pre>
      <h4 id="idm139667869211024">Why does Kawa’s REPL use display rather than write?</h4>
      <p>The read-eval-print-loop of most Scheme implementations prints the
evaluation result using <code class="literal">write</code>, while Kawa uses <code class="literal">display</code> by default.
</p>
      <p>First note that it is easy to override the default with the
<code class="literal">--output-format</code> command-line option:
</p>
      <pre class="screen">$kawa --output-format readable-scheme
#|kawa:1|# "abc"
"abc"
</pre>
      <p>The reason <code class="literal">display</code> is the default is because of a vision of the REPL
console as more than just printing out Scheme objects in
textual form for use by a programmer.
Some examples:
</p>
      <div class="itemizedlist" epub:type="list">
        <ul class="itemizedlist" style="list-style-type: disc; ">
          <li class="listitem" epub:type="list-item">
            <p>A math program can display equations and graphs as the
output of an expression.
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>An expression can evaluate to a "picture" which would
be <a class="ulink" href="http://per.bothner.com/blog/2007/ReplPane/" target="_top">displayed inline</a>.
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>An HTML/XML obj can be insert into the output in visual
form if the console understands HTML.  (There is a prototype
for this that works by using the JavaFX WebView as the display.)
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>The plan for "Kawa-shell" functionality is to have expressions
that evaluate to process objects, which would be lazy strings.
This string would be the data from standard output.  Thus the
effect of displaying a process object would be to print out
the standard output - just like a regular shell.  Users would
find it confusing/annoying if shell output used quotes.
</p>
          </li>
        </ul>
      </div>
      <p>This "repl-as-pad" model doesn’t work as well if the repl
uses <code class="literal">write</code> rather than <code class="literal">display</code>. 
</p>
    </section>
    <footer>
      <div class="navfooter">
        <p>
          Up: <a accesskey="u" href="pt01.xhtml">Part . Reference Documentation</a></p>
        <p>
        Previous: <a accesskey="p" href="Miscellaneous.xhtml">Miscellaneous topics</a></p>
        <p>
        Next: <a accesskey="n" href="Framework.xhtml">The Kawa language framework</a></p>
      </div>
    </footer>
  </body>
</html>
